layui.use(['form','jquery','layer','laydate','upload','element'], function() {
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var element = layui.element;

    laydate.render({
        elem: "#startDate",
        type: "datetime",
        trigger: "click"
    });
    laydate.render({
        elem: "#endDate",
        type: "datetime",
        trigger: "click"
    });

    /*上传图片*/
    var uploadInst = upload.render({
        elem: "#uploadImg",
        url: "/newCoupon/uploadFile",
        multiple: false,
        field: "file",
        accept: "images",
        before: function (obj) {
            $('#images').show();
        },
        done: function (res, index, upload) {
            var pricuteUrl = res.data.path;
            $('#image').html('<img src="'+ "http://"+ pricuteUrl
                + '" height="100px" width="100px" border="1px solid #ccc" class="layui-upload-img uploadImgPreView">');
            $('input[name=pricuteUrl]').val(pricuteUrl);
        },
        error: function () {
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    /*添加优惠券*/
    form.on('submit(sub)', function (data) {
        var createId = localStorage.getItem("userId");
        var data={
            'couponName':$("#couponName").val(),
            'couponPrice':$("#couponPrice").val(),
            'startDate':$("#startDate").val(),
            'endDate':$("#endDate").val(),
            'online':$("input[name=online]").val(),
            'grant':$("#grant").val(),
            'issueQuantity':$("#issueQuantity").val(),
            'type':$("#type").val(),
            'createId':createId,
            'pricuteUrl':$('input[name=pricuteUrl]').val()
        };
        var StartDate = new Date(Date.parse($("#startDate").val()));
        var EndDate = new Date(Date.parse($("#endDate").val()));
        if(StartDate>EndDate){
            alert("活动开始时间不能早于结束时间");
            return;
        }
        $.ajax({
            url:"/coupon/addCoupon",
            type:"post",
            dataType:"json",
            data:data,
            success:function () {
                parent.$("#Jump").val('优惠券列表');
                parent.getQuick();
                return;
            }
        })
    })

    /*数字验证*/
    form.verify({
        price: function (value, item) {
            if (/^-[0-9]*[1-9][0-9]*$/.test(value)) {
                return '价格，数量等不能为空不能为负数';
            }
        }
    });

    /*返回*/
    goBack = function goBack() {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
    /*刷新*/
    Refresh = function Refresh() {
        location.reload();
    };

});